﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using TST.SharePoint.Shared;
using Microsoft.SharePoint.WebPartPages;
using System.IO;
using TST.WebParts.Shared.WebControls;

namespace TST.WebParts
{
    [ViewStateModeById]
    class UserInfoFilterEditorPart : EditorPart
    {
        private Exception _error;
        private SortDropDownList _selectField;

        public UserInfoFilterEditorPart(string webPartID)
        {
            this.ID = "UserInfoFilterEditorPart" + webPartID;
            this.Title = "User Info Filter Options";
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            HtmlTable table = new HtmlTable();
            table.CellPadding = 5;
            table.CellSpacing = 3;
            Controls.Add(table);

            HtmlTableRow rowlist = new HtmlTableRow();
            table.Controls.Add(rowlist);
            HtmlTableCell cellList = new HtmlTableCell();
            rowlist.Controls.Add(cellList);
            Label labelList = new Label();
            labelList.Text = "Select a field";
            cellList.Controls.Add(labelList);
            cellList.Controls.Add(new LiteralControl("<br />"));
            _selectField = new SortDropDownList();
            cellList.Controls.Add(_selectField);
        }

        private void PopulateFields(String selectedUserInfoField)
        {
            SPList infoList = SPContext.Current.Web.SiteUserInfoList;
            bool displayItemFound = false;
            _selectField.Items.Clear();
            _selectField.Items.Add(new ListItem("-- Select a field --", ListItemFilter.EMPTYITEMVALUE));
            foreach (SPField field in infoList.Fields)
            {
                if (!field.Hidden)
                {
                    ListItem item = new ListItem(String.Format("{0} ({1})", field.Title, field.InternalName), field.InternalName);
                    if (field.InternalName.Equals(selectedUserInfoField, StringComparison.InvariantCultureIgnoreCase))
                    {
                        displayItemFound = true;
                    }
                    _selectField.Items.Add(item);

                }
            }
            if (displayItemFound)
            {
                _selectField.SelectedValue = selectedUserInfoField;
            }
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            UserInfoFilter webPart = WebPartToEdit as UserInfoFilter;
            if (webPart != null)
            {
                try
                {
                    _selectField.SelectedValue = webPart.UserInfoField;
                    PopulateFields(webPart.UserInfoField);
                }
                catch (Exception ex)
                {
                    _error = ex;
                }
            }
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();
            UserInfoFilter webPart = WebPartToEdit as UserInfoFilter;
            if (webPart != null)
            {
                webPart.UserInfoField = _selectField.SelectedValue;
            }
            return true;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (_error != null)
            {
                writer.WriteLine(string.Format("<strong>Error:</strong> {0}.", _error.Message));
            }
            base.RenderContents(writer);
        }

    }
}
